//
//  CZTabBarViewController.swift
//  微博
//
//  Created by 范承志 on 16/6/11.
//  Copyright © 2016年 fanchengzhi. All rights reserved.
//

import UIKit
import SVProgressHUD

class CZTabBarViewController: UITabBarController {
 
    override func viewDidLoad() {
        super.viewDidLoad()

        //实例化tabbar
        let tabBar = CZTabBar()
        
        //第二部：实例化闭包
        tabBar.closure = {
            //第四部：接收回调
            if !CZUserAccountViewModel.sharedTools.isLogin {
                
                SVProgressHUD.showError(withStatus: "请先登录，亲！~")
                
                return
            }
            
            //代码执行到此，表示登录
            //获取Window 获取最上面的window
            let window = UIApplication.shared.windows.last!
            
            let composeView = CZComposeView()
            
            window.addSubview(composeView)
            
        }
        
        //通过KVC给计算性属性
        setValue(tabBar, forKey: "tabBar")
        
        //添加子控制器(主页、消息、发现、我的)
        addChildViewController(vc: CZHomeViewController(), title: "主页", imageName: "tabbar_home")
        addChildViewController(vc: CZMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(vc: CZDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(vc: CZProfileViewController(), title: "我的", imageName: "tabbar_profile")
    }
    
    //抽取创建子控制器的方法
    
    /// 创建子控制器
    ///
    /// - parameter vc:        控制器
    /// - parameter title:     标题
    /// - parameter imageName: 图片
    private func addChildViewController(vc: UIViewController, title: String, imageName: String){
        
        //设置title
        vc.title = title
        //设置文字的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : CZTHEMECOLOR], for: UIControlState.normal)
        //设置默认图片
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置选中图片拼接并且设置选中图片的原声渲染方式
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置根控制器
        let navVc = CZNavigationViewController(rootViewController: vc)
        //添加到导航控制器
        addChildViewController(navVc)
        
    }

  }
